/*
*
* 封装的js方法
*
* */
(function ($) {
    $.extend({
        alert: function (msg) {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.msg(msg);
            });
        },
        //msg: 提示信息, callback: 回调方法, code: icon 默认1为成功, 2为失败 ，timeout: 自动关闭时间，默认2秒，
        msg: function (msg, callback, code, timeout) {
            layui.use('layer', function () {
                code = code || 1;
                timeout = timeout || 2000;
                var layer = layui.layer;
                if(!msg){
                    msg ='后台出错，请联系系统管理员';
                }
                layer.msg(msg, {
                    icon: code,
                    time: timeout //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    callback();
                });
            });
        },
        //url: 请求地址, data: 请求数据{}, success: 请求成功的回调方法, async: 是否异步请求，默认true, pleaseWait: 是否显示请等待提示，默认true
        cajax: function (url, data, success, async, pleaseWait) {
            layui.use('layer', function () {
                var layer = layui.layer;
                data = data || {};
                async = async || true;
                pleaseWait = async || true;
                var index;
                $.ajax({
                    url: url,
                    dataType: 'json',
                    type: 'POST',
                    async: async,
                    beforeSend: function (XHR) {
                        if (pleaseWait) {
                            index = layer.load(3);
                        }
                    },
                    complete: function (XHR, TS) {
                        if (pleaseWait) {
                            layer.close(index);
                        }
                    },
                    data: data,
                    success: success,
                    error: function (e) {
                        console.info(e);
                        $.alert('服务器错误，请联系管理员');
                    }
                });
            });
        },
        toHref: function (url) {
            window.location.href = url;
        },
        loadContent: function (url, title, data, area, offset, btn, success) {
            data = data || {};
            area = area || ['300px', '300px'];
            offset = offset || [];
            btn = btn || [];
            var index;
            $.ajax({
                url: url,
                dataType: 'html',
                data: data,
                type: 'post',
                beforeSend: function (XHR) {
                    index = layer.load(3);
                },
                complete: function (XHR, TS) {
                    layer.close(index);
                },
                success: function (data) {
                    var html = $(data);
                    layer.open({
                        btn: btn,
                        area: area,
                        offset: offset,
                        title: title,
                        type: 1,
                        resize: false,
                        content: html.html(), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        yes: function (index, layero) {
                            if (success) {
                                success();
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                        },
                        btn3: function (index, layero) {
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                },
                error: function (e) {
                    console.info(e);
                }
            });
        },
        /**
         * 确认框
         * title   标题
         * url     请求的url
         * width   弹出层宽度（缺省调默认值）
         height  弹出层高度（缺省调默认值）
         * callback 回调函数
         * cancel 取消事件
         */
        x_admin_Confirmation_Box_Show : function (title, url, width, height, callback, cancel) {
            if (title == null || title == '') {
                title=false;
            };
            if (url == null || url == '') {
                url="404.html";
            };
            if (width == null || width == '') {
                width=($(window).width()*0.9);
            };
            if (height == null || height == '') {
                height=($(window).height() - 50);
            };
            layer.open({
                type: 2,
                title: title,
                shadeClose: false,
                shade: 0.8,
                btnAlign: 'c',
                area:[width+'px', height +'px'],
                btn:['保存','取消'],
                yes:function(index,layero){
                    if(callback){
                        callback(index, layero);
                    }
                },
                btn2:function(index,layero){
                    if(cancel){
                        cancel();
                    }
                },
                content:url
            });
        }
    });
})(window.jQuery);

//将第一个逗号分隔参数中包含的第二个参数的字符串去掉
function removeStr(str, index) {
    str = (str + ',').replace((index + ','), '');
    //去掉最后所有逗号的正则
    var reg = /,$/gi;
    str = str.replace(reg, '');
    return str;
}